package jsu.blogger.util;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import jsu.blogger.pojo.LogRecord;
import jsu.blogger.pojo.Message;
import jsu.blogger.pojo.WebSocketMessage;

import java.time.LocalDateTime;



public class MessageUtil {

    /**
     * 创建一个返回消息对象
     *
     * @param messageId  消息的唯一标识
     * @param senderId   发送者的用户ID
     * @param receiverId 接收者的用户ID
     * @param content    消息内容
     * @return 返回一个JSON格式化的消息字符串
     */
    public static String toJsonMessage(Long messageId, Long senderId, Long receiverId, String content,LocalDateTime localDateTime) {
        Message message = new Message();
        message.setMessageId(messageId);
        message.setSenderId(senderId);
        message.setReceiverId(receiverId);
        message.setContent(content);
        message.setCreatedAt(localDateTime);
        message.setIsAccepted(false);
        return JSON.toJSONString(message);
    }
    public static String toJsonWebSocketMessage(int code, String content,Object data) {
        return JSON.toJSONString(new WebSocketMessage(code, content, data));
    }
    /**
     * 反格式化消息为Message对象
     */
    public static WebSocketMessage parseJsonToWebSocketMessage(String jsonWebSocketString) throws Exception {
        return JSON.parseObject(jsonWebSocketString, WebSocketMessage.class);
    }
    public static Message parseJsonToMessage(JSONObject jsonString) throws Exception {
        return jsonString.toJavaObject(Message.class);
    }
    public static LogRecord parseJsonToLogRecord(JSONObject jsonString) throws Exception {
        return jsonString.toJavaObject(LogRecord.class);
    }
}
